package com.baizhi.cmfzmanager.appcontroller;

import com.baizhi.cmfzmanager.entity.User;
import com.baizhi.cmfzmanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class AppUserController {
    @Autowired
    private UserService userService;

    @PostMapping("/userlogin")
    public User userlogin(String userTelephone,String userPassword){

        return userService.login(userTelephone,userPassword);
    }

    //修改用户名
    @PostMapping("/updateUsername")
    public Map updateUsername( Integer userId, String username){
        Map map = new HashMap();
        try {
            userService.updateUsername(userId,username);
            map.put("message",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message",false);
        }

        return map;
    }

    @PostMapping("/updateUserImage")
    public Map updateUserImage(Integer userId, MultipartFile userImage){
        Map map = new HashMap();
        try {
            String filename = userImage.getOriginalFilename();
            userImage.transferTo(new File("d://zask/"+filename));
            userService.updateUserImage(userId,filename);
            map.put("message",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message",false);
        }

        return map;
    }

    @PostMapping("/updateNickname")
    public Map updateNickname( Integer userId, String userNickname){
        Map map = new HashMap();
        try {
            userService.updateNickname(userId,userNickname);
            map.put("message",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message",false);
        }

        return map;
    }
}
